<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="60%" append-to-body custom-class="import-dialog">
      <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent >
          <el-form-item label="小区" prop="estateName">
            <el-input v-model="queryParams.estateName" placeholder="请输入小区" clearable style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="楼栋" prop="buildingName">
            <el-input v-model="queryParams.buildingName" placeholder="请输入楼栋" clearable style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="房间号" prop="roomNumber">
            <el-input v-model="queryParams.roomNumber" placeholder="请输入房间号" clearable style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" ref="table">
          <el-table-column label="楼盘" align="center" prop="house.estateName"/>
          <el-table-column label="楼栋" align="center" prop="house.buildingName"/>
          <el-table-column label="单元" align="center" prop="house.unitName"/>
          <el-table-column label="房间号" align="center" prop="house.roomNumber"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button @click="selectHosue(scope.row)" :type="ids.includes(scope.row.houseId) ? 'primary' : ''">
                {{ ids.includes(scope.row.houseId) ? '已选择' : '选择' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
      <div slot="footer" class="dialog-footer" v-if="isMultiple">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listAllocation } from '@/api/house/price'
import { debounce } from '@/utils'

export default {
  props: {
    title: {
      default: '选择房间'
    },
    value: {
      default: false
    },
    selectedData: {
      default: () => []
    },
    isMultiple: {
      default: false
    }
  },
  dicts: [],
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        estateName: undefined,
        buildingName: undefined,
        roomNumber: undefined,
        apartmentType: undefined,
        editStatus: undefined,
        apartmentState: undefined,
        updateBy: undefined
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
      // 选中列表
      selectedList: []
    }
  },
  computed: {
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },

  },
  watch: {
    value: {
      handler(newVal) {
        if (newVal) {
          this.init()
        }
      },
      immediate: true
    }
  },
  methods: {
    init() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        estateName: undefined,
        buildingName: undefined,
        roomNumber: undefined,
        apartmentType: undefined,
        editStatus: undefined,
        apartmentState: undefined,
        updateBy: undefined
      }
      this.ids = this.selectedData.map(item => item.houseId)
      this.selectedList = [...this.selectedData]

      this.getList()
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置表单
    resetQuery() {
      // this.dateRange = [];
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 查询房间列表 */
    getList() {
      this.loading = true
      listAllocation(this.queryParams).then((response) => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleSelectionChange() {
    },
    /** 选择房屋 */
    selectHosue(item) {
      if (this.isMultiple) {
        if (this.ids.includes(item.houseId)) {
          this.ids.splice(this.ids.indexOf(item.houseId), 1)
          this.selectedList.splice(this.selectedList.indexOf(item), 1)
        } else {
          this.ids.push(item.houseId)
          this.selectedList.push(item)
        }
      } else {
        this.ids = [item.houseId]
        this.selectedList = [item]
        this.$emit('success',this.selectedList)
        this.close()
      }

    },
    /** 确定 */
    submit: debounce(function() {
      if (this.isMultiple) {
        if (this.ids.length === 0) {
          this.$message.error('请选择房屋')
          return
        }
        this.$emit('success', this.selectedList)
        this.close()
      }

    }, 500, true),

    /** 关闭弹窗 */
    close() {
      this.resetQuery()
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/styles/dialog.scss";
</style>
